<?php

namespace app\api\controller;

use app\api\server\Cart as ServerCart;
use app\common\controller\Api;
use app\common\model\Article;
use app\common\model\Cart as ModelCart;
use app\common\model\goods\Category;
use app\common\model\User;

/**
 * 购物车接口
 */
class Cart extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];



    /**
     * @Author LK
     * @ApiTitle (添加/编辑购物车)
     * @ApiMethod (POST)
     * @ApiParams (name="goods_id", type="integer", required=true, sample="", description="商品ID")
     * @ApiParams (name="num", type="integer", required=true, sample="", description="数量")
     * @ApiParams (name="unit", type="string", required=true, sample="", description="单位")
     * @ApiParams (name="add_sub", type="string", required=true, sample="", description="0-编辑；1-增加；2-减少")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function add_cart()
    {
        $params = request()->param();
        try {
            ServerCart::addCart($params, $this->auth->id, 1);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('请求成功');
    }

    /**
     * @Author LK
     * @date: 2023-11-13 
     * @ApiTitle (删除购物车)
     * @ApiMethod (POST)
     * @ApiParams (name="ids", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function del_cart()
    {
        $params = request()->param();
        extract($params);
        if (!isset($ids) || !$ids) {
            $this->error('参数错误');
        }
        $res = ModelCart::whereIn('id', $ids)->delete();
        if ($res) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    /**
     * @Author LK
     * @date: 2023-11-13 
     * @ApiTitle (购物车列表)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function cart_list()
    {
        $lists = ServerCart::lists($this->auth->id);
        $this->success('', $lists);
    }
}
